View Javadoc

1   package net.sourceforge.simplegamenet.connectaline;
2   
3   import java.io.Serializable;
4   import net.sourceforge.simplegamenet.specs.model.GameServer;
5   import net.sourceforge.simplegamenet.specs.model.ServerEngine;
6   import net.sourceforge.simplegamenet.specs.to.PlayerSettings;
7   
8   public class CALServer extends GameServer implements CALPacketCodes,
9           CALPlayStyle {
10  
11      private CALPlayField cALPlayField = null;
12      private Integer[] originPlayerIDs;
13  
14      public CALServer(ServerEngine serverEngine) {
15          super(serverEngine);
16      }
17  
18      public void playerAdded(PlayerSettings playerSettings) {
19          if (serverEngine.isGamePlaying()) {
20              serverEngine.sendData(playerSettings.getPlayerID(),
21                      new CALPacket(PLAY_FIELD, cALPlayField));
22          }
23      }
24  
25      public void playerRemoved(PlayerSettings playerSettings) {
26          if (serverEngine.isGamePlaying()
27                  && playerSettings.getPlayingState() == PlayerSettings.PARTICIPATING) {
28              serverEngine.appendGameChatMessage("The game is remise.");
29              serverEngine.stopGame();
30          }
31      }
32  
33      public void gameStarted() {
34          int originPlayerIDsLength;
35          int playStyle;
36          int randomNumber;
37          CALSettings cALSettings;
38          Integer[] participantsOrder;
39  
40          cALSettings = (CALSettings) serverEngine.getGameSettings();
41          originPlayerIDs = serverEngine.getPlayerSettingsMap()
42                  .getParticipantIDs();
43          participantsOrder = new Integer[originPlayerIDs.length];
44          originPlayerIDsLength = originPlayerIDs.length;
45          for (int i = 0; i < participantsOrder.length; i++) {
46              randomNumber = (int) (Math.random() * (originPlayerIDsLength));
47              participantsOrder[i] = originPlayerIDs[randomNumber];
48              for (int j = randomNumber; j < originPlayerIDsLength - 1; j++) {
49                  originPlayerIDs[j] = originPlayerIDs[j + 1];
50              }
51              originPlayerIDsLength--;
52          }
53          cALPlayField = new CALPlayField(cALSettings.getNumberOfColumns(),
54                  cALSettings.getNumberOfRows(),
55                  cALSettings.getLengthOfALine(),
56                  participantsOrder);
57          CALPacket cALPacket = new CALPacket(INIT, participantsOrder);
58          sendCALPacket(cALPacket);
59      }
60  
61      public void receiveData(Integer playerID, Serializable data) {
62          CALPacket cALPacket = (CALPacket) data;
63          CALSettings cALSettings = (CALSettings) serverEngine.getGameSettings();
64          switch (cALPacket.getCalPacketCode()) {
65              case RESULT_TURN_BY_PLAYER:
66                  int[] coordinates = (int[]) cALPacket.cALData;
67                  switch (cALSettings.getPlayStyle()) {
68                      case GRAVITY:
69                          if (cALPlayField.isMoveAllowedGravity(coordinates[0],
70                                  coordinates[1])) {
71                              cALPlayField.makeMove(coordinates[0], coordinates[1],
72                                      playerID);
73                              if (cALPlayField.isWinner(coordinates[0],
74                                      coordinates[1])) {
75                                  sendCALPacket(new CALPacket(WINNER, coordinates));
76                                  serverEngine.appendGameChatMessage(serverEngine.getPlayerSettingsMap()
77                                          .getPlayerSettings(playerID).getNickname()
78                                          + " has won the game.");
79                                  serverEngine.stopGame();
80                              } else if (cALPlayField.isRemise()) {
81                                  sendCALPacket(new CALPacket(REMISE, coordinates));
82                                  serverEngine.appendGameChatMessage("Remise");
83                                  serverEngine.stopGame();
84                              } else {
85                                  sendCALPacket(new CALPacket(MAKE_MOVE_BY_PLAYER,
86                                          coordinates));
87                              }
88                          }
89                          break;
90                      case ANY_EMPTY_SPOT:
91                          if (cALPlayField.isMoveAllowedAnyEmptySpot(coordinates[0],
92                                  coordinates[1])) {
93                              cALPlayField.makeMove(coordinates[0], coordinates[1],
94                                      playerID);
95                              if (cALPlayField.isWinner(coordinates[0],
96                                      coordinates[1])) {
97                                  sendCALPacket(new CALPacket(WINNER, coordinates));
98                                  serverEngine.appendGameChatMessage(serverEngine.getPlayerSettingsMap()
99                                          .getPlayerSettings(playerID).getNickname()
100                                         + " has won the game.");
101                                 serverEngine.stopGame();
102                             } else if (cALPlayField.isRemise()) {
103                                 sendCALPacket(new CALPacket(REMISE, coordinates));
104                                 serverEngine.appendGameChatMessage("Remise");
105                                 serverEngine.stopGame();
106                             } else {
107                                 sendCALPacket(new CALPacket(MAKE_MOVE_BY_PLAYER,
108                                         coordinates));
109                             }
110                         }
111                         break;
112                 }
113                 break;
114             case RESULT_TURN_BY_CLIENT:
115                 break;
116             default:
117                 System.out.println("Unrecognized packet");
118         }
119     }
120 
121     public void sendCALPacket(CALPacket cALPacket) {
122         serverEngine.sendData(cALPacket);
123     }
124 
125     public void gameAborted() {
126         serverEngine.appendGameChatMessage("Remise");
127     }
128 }